package com.aos.eap.activity;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.adapter.SearchThingListAdapter;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.SearchThingBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class SearchThingActivity extends BaseDialogActivity {


    @BindView(R.id.rvThingInfo)
    RecyclerView rvThingInfo;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBatchId)
    TextView tvBatchId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvStartTime)
    TextView tvStartTime;
    @BindView(R.id.tvQty)
    TextView tvQty;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvUser)
    TextView tvUser;

    private SearchThingListAdapter searchThingListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search_thing;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        rvThingInfo.setLayoutManager(new LinearLayoutManager(SearchThingActivity.this));
        rvThingInfo.setItemAnimator(new DefaultItemAnimator());
        rvThingInfo.addItemDecoration(new DividerItemDecoration(SearchThingActivity.this, 1));
        searchThingListAdapter = new SearchThingListAdapter(null);
        searchThingListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        searchThingListAdapter.isFirstOnly(true);
        rvThingInfo.setAdapter(searchThingListAdapter);
    }

    @Override
    protected void initData() {
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_MTMSEARCH", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<SearchThingBean>>(
                new TypeToken<BaseResponseBean<SearchThingBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<SearchThingBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    setPageData(response.body().getResponse().getBody());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<SearchThingBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_MTMSEARCH");
            }
        });
    }

    private void setPageData(SearchThingBean searchThingBean) {
        tvLotId.setText(searchThingBean.getMtmhead().getLotid());
        tvBatchId.setText(searchThingBean.getMtmhead().getBatchid());
        tvStartTime.setText(searchThingBean.getMtmhead().getStarttime());
        tvQty.setText(String.valueOf(searchThingBean.getMtmhead().getDealcount()));
        searchThingListAdapter.setNewData(searchThingBean.getMtmbodies());
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("物料信息查询");
        freeUI_titleView.setLeftListener(view -> showBackDialog(SearchThingActivity.this));
        freeUI_titleView.setRightListener(R.drawable.refresh, v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            String eqpId = etEqpId.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(eqpId)) {
                showToast("请扫描设备号", false);
                return;
            }
            requestInfoByEqpId(eqpId);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }
}
